package com.asteros.crypto.utils.content;

import com.asteros.common.util.io.IOUtil;
import com.asteros.crypto.utils.content.remote.client.RemoteCMWebServiceProxy;

public final class RemoteCM_ws implements ContentManager
{
	protected static final String CONTENT_PATH 		= "C:\\Projects\\RKK\\testdata\\_data\\user\\";
	protected static final String CONTENT_FILE_NAME = CONTENT_PATH + "b.txt";
	//	protected static final String CONTENT_FILE_NAME = CONTENT_PATH + "content.pdf";
	protected static final String SIGN_FILE_NAME 	= CONTENT_PATH + "2.sgn";

	private RemoteCMWebServiceProxy wsClient ;//= new RemoteCMWebServiceProxy(getWSUrl());
	public RemoteCM_ws() {
		System.out.println("RemoteCM_ws.constructor>>");
		wsClient = new RemoteCMWebServiceProxy(getWSUrl());
		System.out.println("RemoteCM_ws.constructor<<");
	}
	public byte[] getContent(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteCM.getContent(applet-ws)>>");

		byte[] response = wsClient.getContent(userId, documentId);
		System.out.println(new String(response));
		IOUtil.saveFile(response, CONTENT_FILE_NAME);

		System.out.println("RemoteCM.getContent.size=" + response.length);

		System.out.println("RemoteCM.getContent(applet-ws)<<");
		return response;
	}

	public boolean verifyDocumentSignature(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteCM.verifyDocumentSignature(applet-ws)>>");

		boolean res = wsClient.verifyDocumentSignature(userId, documentId);
		System.out.println("RemoteCM.verifyDocumentSignature.size=" + res);

		System.out.println("RemoteCM.verifyDocumentSignature(applet-ws)<<");
		return res;	
	}

	public byte[] getSignedData(String userId, String documentId) throws Exception
	{
		System.out.println("RemoteCM.getSignedData(applet-ws)>>");

		byte[] buf = wsClient.getSignedData(userId, documentId);

		System.out.println("RemoteCM.getContent.size=" + buf.length);

		IOUtil.saveFile(buf, SIGN_FILE_NAME);

		System.out.println("RemoteCM.getSignedData(applet-ws)<<");
		return buf;
	}

	public void saveSignedData(String userId, String documentId, byte[] signedData) throws Exception {
		System.out.println("RemoteCM.saveSignedData(applet-ws)>>buf.len" + signedData.length);

		wsClient.saveSignedData(userId, documentId, signedData);

		System.out.println("RemoteCM.saveSignedData(applet-ws)<<");
	}

	private String getWSUrl()
	{
		return "http://localhost:8080/RKK_EDS_WS/services/RemoteCMWebService";
	}
}
